using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Timers;
using Agresso.Interface.Layer.Service;
using Agresso.Interface.Layer.Domain;
using System.Collections;
using System.Configuration;

namespace Agresso.Interface.Layer.WinService.Production
{
    public class AgressoInterfaceLayerWinService : ServiceBase
    {
        private static EventLog evt = new EventLog("Application");
        public static string SVC_NAME = "AgressoInterfaceLayerWinService.Production";

        private DateTime m_lastExecuteDateTCRelet;
        private bool m_ExecuteDateTCRelet = false;

        private DateTime m_lastExecuteDateProforma;
        private bool m_ExecuteDateProforma = false;

        private DateTime m_lastExecuteDateVoyageCargoRelet;

        private DateTime m_lastExecuteDateFreightHireInvoice;
        private bool m_ExecuteDateFreightHireInvoice = false;

        private DateTime m_lastExecuteDateVoyageMaster;
        private bool m_ExecuteDateVoyageMaster = false;

        private DateTime m_lastExecuteDateBunkerPO;
        private bool m_ExecuteDateBunkerPO = false;
        
        private DateTime m_lastExecuteDateBunkerGRN;
        private bool m_ExecuteDateBunkerGRN = false;

        private DateTime m_lastExecuteDateWinSDSPO;
        private bool m_ExecuteDateWinSDSPO = false;

        private DateTime m_lastExecuteDateStatement;
        private bool m_ExecuteDateStatement = false;

        private DateTime m_lastExecuteDateEmailAlert;
        private bool m_ExecuteDateEmailAlert = false;

        private DateTime m_lastExecuteDateBunkerInventory;
        private bool m_ExecuteDateBunkerInventory = false;

        private DateTime m_lastExecuteDateCrewPayroll;
        private bool m_ExecuteDateCrewPayroll = false;


        public AgressoInterfaceLayerWinService()
		{
			this.ServiceName=SVC_NAME;
		}
        public AgressoInterfaceLayerWinService(string[] args)//testing--------------
        {
            this.ServiceName = SVC_NAME;
            OnStart(args);
        }
        static void Main(string[] args)
        {
            evt.Source = SVC_NAME;
            ServiceBase.Run(new AgressoInterfaceLayerWinService());
        }
        protected override void OnStart(string[] args)
        {
            TaskSchedule ts = new TaskSchedule("TCRelet", 15000, new TaskSchedule.Job(ExecuteCommissionInvoiceTCReletType));
            TaskSchedule prof = new TaskSchedule("Proforma", 15000, new TaskSchedule.Job(ExecuteCommissionInvoiceProforma));
            TaskSchedule tvc = new TaskSchedule("VoyageCargoRelet", 15000, new TaskSchedule.Job(ExecuteCommissionInvoiceVoyageCargoReletType));
            TaskSchedule tfh = new TaskSchedule("FreightHireInvoice", 15000, new TaskSchedule.Job(ExecuteCommissionInvoiceFreightHireType));
            TaskSchedule winsdsPO = new TaskSchedule("WinSDSPO", 15000, new TaskSchedule.Job(ExecuteWinSDSPO));
            TaskSchedule statement = new TaskSchedule("Statement", 15000, new TaskSchedule.Job(ExecuteStatement));
            TaskSchedule voyageMaster = new TaskSchedule("VoyageMaster", 15000, new TaskSchedule.Job(ExecuteVoyageMaster));
            TaskSchedule bunkerPO = new TaskSchedule("BunkerPO", 15000, new TaskSchedule.Job(ExecuteBunkerPO));
            TaskSchedule bunkerGRN = new TaskSchedule("BunkerGRN", 15000, new TaskSchedule.Job(ExecuteBunkerGRN));
            TaskSchedule emailAlert = new TaskSchedule("EmailAlert", 15000, new TaskSchedule.Job(ExecuteEmailAlert));
            TaskSchedule bunkerInventory = new TaskSchedule("BunkerInventory", 15000, new TaskSchedule.Job(ExecuteBunkerInventory));
            TaskSchedule crewPayroll = new TaskSchedule("CrewPayroll", 15000, new TaskSchedule.Job(ExecuteCrewPayroll));
            EmailService.SendFatalErrorToDeveloper("PCL Interface Production System", "Service is started at " + DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss"), "widagdo.baskoro2@ddw-paxocean.com");            
        }
        protected override void OnStop()
        {
            base.OnStop();
            EmailService.SendFatalErrorToDeveloper("PCL Interface Production System", "Service is stopped at " + DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss"), "widagdo.baskoro2@ddw-paxocean.com");            
        }
        private void ExecuteCommissionInvoiceTCReletType()
        {
            if (m_ExecuteDateTCRelet) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("TCRelet");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateTCRelet = true;
                
                bool lastexecute = m_lastExecuteDateTCRelet == time;
                if (!lastexecute)
                {
                    CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
                    evt.WriteEntry(string.Format("Execute Import Commission Invoice TC Relet Type Interface To Interface Temp Table on {0}", DateTime.Now));
                    commissionInvoiceService.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_COMMISSION_INVOICE_TCRELET);//.ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_COMMISSION_INVOICE_TCRELET);
                    evt.WriteEntry(string.Format("Execute Import Commission Invoice TC Relet Type Interface To Interface Temp Table on {0} Succeed", DateTime.Now));
                    evt.WriteEntry(string.Format("Execute Commission Invoice TC Relet Type Interface on {0}", DateTime.Now));
                    commissionInvoiceService.ExecuteCommissionInvoiceTCReletType();
                    commissionInvoiceService.ExecuteCommissionInvoiceTCReletTypeChargeDebit();
                    m_lastExecuteDateTCRelet = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute Commission Invoice TC Relet Type Interface on {0} done", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute Commission Invoice TCReletType", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute Commission Invoice TCReletType : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateTCRelet = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteCommissionInvoiceProforma()
        {
            if (m_ExecuteDateProforma) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("Proforma");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateProforma = true;
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateProforma == time;
                if (!lastexecute)
                {
                    CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
                    evt.WriteEntry(string.Format("Execute Import Commission Invoice Proforma Type Interface To Interface Temp Table on {0}", DateTime.Now));
                    commissionInvoiceService.ClearAllProformaTransaction();
                    commissionInvoiceService.ImportAllDataSoftmarToMiddleLayerProforma();
                    evt.WriteEntry(string.Format("Execute Import Commission Invoice Proforma Type Interface To Interface Temp Table on {0} Succeed", DateTime.Now));
                    evt.WriteEntry(string.Format("Execute Commission Invoice Proforma Interface on {0}", DateTime.Now));
                    //commissionInvoiceService.ExecuteInvoiceProformaChargeCredit();
                    //commissionInvoiceService.ExecuteInvoiceProformaChargeDebit();
                    m_lastExecuteDateProforma = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute Commission Invoice Proforma Type Interface on {0} done", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute Commission Invoice Proforma", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute Commission Invoice Proforma : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateProforma = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteStatement()
        {
            if (m_ExecuteDateStatement) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("Statement");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateStatement = true;
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateStatement == time;
                if (!lastexecute)
                {
                    StatementService statementService = new StatementService();
                    evt.WriteEntry(string.Format("Execute Import Statement Interface To Interface Temp Table on {0}", DateTime.Now));
                    statementService.ImportAllDataSoftmarToMiddleLayer(0);
                    statementService.ImportAllDataSoftmarToMiddleLayer(1);
                    evt.WriteEntry(string.Format("Execute Import Statement Interface To Interface Temp Table on {0} Succeed", DateTime.Now));
                    evt.WriteEntry(string.Format("Execute Statement Interface on {0}", DateTime.Now));
                    statementService.ExecuteStatementInterface();
                    m_lastExecuteDateStatement = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute Statement Interface on {0} done", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute Statement", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute Statement : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateStatement = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteCommissionInvoiceVoyageCargoReletType()
        {
            try
            {
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                //bool lastexecute = m_lastExecuteDateVoyageCargoRelet == time;
                //if (!lastexecute)
                //{
                //    CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
                //    evt.WriteEntry(string.Format("Execute Import Commission Invoice VC Relet Type Interface To Interface Temp Table on {0}", DateTime.Now));
                //    commissionInvoiceService.ImportAllDataSoftmarToMiddleLayer();//.ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_COMMISSION_INVOICE_VOYAGE_CARGO_RELET);
                //    evt.WriteEntry(string.Format("Execute Import Commission Invoice VC Relet Type Interface To Interface Temp Table on {0} Succeed", DateTime.Now));
                //    evt.WriteEntry(string.Format("Execute Commission Invoice VC Relet Type Interface on {0}", DateTime.Now));
                //    commissionInvoiceService.ExecuteCommissionInvoiceVCReletType();
                //    ConfigurationSettingService confService = new ConfigurationSettingService();
                //    ConfigurationSetting cs = confService.GetConfigurationSetting("VoyageCargoRelet");
                //    cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                //    confService.UpdateConfiguration(cs);
                //    m_lastExecuteDateVoyageCargoRelet = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                //}
            }
            catch (Exception x)
            {
                evt.WriteEntry(string.Format("Error in Production System while Execute  Commission Invoice VC Relet Type : {0}", x.Message));
            }
        }
        private void ExecuteCommissionInvoiceFreightHireType()
        {
            if (m_ExecuteDateFreightHireInvoice) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("FreightHireInvoice");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateFreightHireInvoice = true;
               // DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateFreightHireInvoice == time;
                if (!lastexecute)
                {
                    CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
                    evt.WriteEntry(string.Format("Execute Import Commission Invoice Freight Hire To Interface Temp Table on {0}", DateTime.Now));
                    commissionInvoiceService.ImportAllDataSoftmarToMiddleLayer(SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);// ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);
                    evt.WriteEntry(string.Format("Execute Import Commission Invoice  Freight Hire To Interface Temp Table on {0} Succeed", DateTime.Now));
                    evt.WriteEntry(string.Format("Execute Commission Invoice  Freight Hire on {0}", DateTime.Now));
                    commissionInvoiceService.ExecuteFreightHireInvoiceCargoReletChargeDebitIN();
                    commissionInvoiceService.ExecuteFreightHireInvoiceCargoReletChargeCreditOUT();
                    commissionInvoiceService.ExecuteFreightHireInvoiceCargoReletCommissionCreditIN();
                    m_lastExecuteDateFreightHireInvoice = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute Commission Invoice  Freight Hire on {0} done", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute Commission Invoice  Freight Hire", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute Commission Invoice  Freight Hire  : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateFreightHireInvoice = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteVoyageMaster()
        {
            if (m_ExecuteDateVoyageMaster) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("VoyageMaster");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime timeNow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == timeNow) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateVoyageMaster = true;
                //DateTime timeNow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateVoyageMaster == timeNow;
                if (!lastexecute)
                {
                    VoyageMasterService vms = new VoyageMasterService();
                    evt.WriteEntry(string.Format("Execute ImportVoyageMaster To Interface Temp Table on {0}", DateTime.Now));
                    vms.ImportAllDataSoftmarToMiddleLayer();// ImportDataSoftmarToMiddleLayer(DateTime.Today.AddDays(-1), SoftmarType.SOFTMAR_FREIGHT_HIRE_INVOICE);
                    evt.WriteEntry(string.Format("Execute ImportVoyageMastere To Interface Temp Table on {0} Succeed", DateTime.Now));
                    evt.WriteEntry(string.Format("Execute Interfacing VoyageMaster on {0}", DateTime.Now));
                    vms.ExecuteVoyageMasterInterface();
                    m_lastExecuteDateVoyageMaster = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute Interfacing VoyageMaster on {0} done", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute Voyage Master", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute Voyage Master : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateVoyageMaster = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteBunkerPO()
        {
            if (m_ExecuteDateBunkerPO) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("BunkerPO");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateBunkerPO = true;
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateBunkerPO == time;
                if (!lastexecute)
                {
                    evt.WriteEntry(string.Format("ImportAllDataSoftmarToMiddleLayer Bunker PO Interface on {0}", DateTime.Now));
                    BunkerPOService bunkerpoService = new BunkerPOService();
                    bunkerpoService.ImportAllDataSoftmarToMiddleLayer();
                    evt.WriteEntry(string.Format("ImportAllDataSoftmarToMiddleLayer Bunker PO done on {0}", DateTime.Now));
                    evt.WriteEntry(string.Format("Execute Bunker PO Interface start on {0}", DateTime.Now));
                    bunkerpoService.ExecuteBunkerProcurementPO();
                    bunkerpoService.UpdateApodettextOrderID();
                    m_lastExecuteDateBunkerPO = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute Bunker PO Interface end on {0}", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute BunkerPO", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute BunkerPO : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateBunkerPO = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteBunkerGRN()
        {
            if (m_ExecuteDateBunkerGRN) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("BunkerGRN");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateBunkerGRN = true;
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateBunkerGRN == time;
                if (!lastexecute)
                {
                    evt.WriteEntry(string.Format("Execute Bunker GRN Interface on {0}", DateTime.Now));
                    BunkerGRNService bunkergrnService = new BunkerGRNService();
                    bunkergrnService.ImportAllDataSoftmarToMiddleLayer();
                    bunkergrnService.ExecuteBunkerGRNByPO();
                    m_lastExecuteDateBunkerGRN = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute BunkerGRN ", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute BunkerGRN : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateBunkerGRN = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteWinSDSPO()
        {
            if (m_ExecuteDateWinSDSPO) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("WinSDSPO");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateWinSDSPO = true;
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateWinSDSPO == time;
                if (!lastexecute)
                {
                    WinSDSService winsdsService = new WinSDSService();
                    evt.WriteEntry(string.Format("Execute WinSDS PO Interface on {0}", DateTime.Now));
                    winsdsService.ImportSupplierDataFromAgressoToDMZ();
                    winsdsService.ExecuteInterfaceAllPOData();
                    winsdsService.UpdatePOID();
                    winsdsService.UpdatePOApprovedDate(); 
                    winsdsService.UpdateAlgbatchinputRemark();
                    winsdsService.UpdatePOIDMiddleLayer();
                    evt.WriteEntry(string.Format("Execute WinSDS PO Interface on {0} done", DateTime.Now));                   
                    m_lastExecuteDateWinSDSPO = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute WinSDS PO", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute WinSDS PO : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateWinSDSPO = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteEmailAlert()
        {
            if (m_ExecuteDateEmailAlert) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("EmailAlert");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateEmailAlert = true;
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateEmailAlert == time;
                if (!lastexecute)
                {
                    evt.WriteEntry(string.Format("Execute Email Alert on {0}", DateTime.Now));

                    UserService m = new UserService();
                    CommissionInvoiceService commissionInvoiceService = new CommissionInvoiceService();
                    StatementService statementService = new StatementService();
                    BunkerPOService bunkerPOService = new BunkerPOService();
                    BunkerGRNService bunkerGRNService = new BunkerGRNService();
                    BunkerInventoryService bunkerInventoryService = new BunkerInventoryService();
                    VoyageMasterService voyageService = new VoyageMasterService();
                    WinSDSService winsdsService = new WinSDSService();

                    int pendingSoftmarCommissionInvoice = commissionInvoiceService.GetAllPendingTCReletCommission().Count;
                    int pendingSoftmarFreightHireInvoice = commissionInvoiceService.GetAllPendingFreightHireCommission().Count;
                    int pendingSoftmarStatement = statementService.GetAllPendingStatement().Count;
                    int pendingSoftmarBunkerPO = bunkerPOService.GetAllPendingPendingBunkerPO().Count;
                    int pendingSoftmarBunkerGRN = bunkerGRNService.GetAllPendingPendingPendingPO().Count;
                    int pendingSoftmarBunkerInventory = bunkerInventoryService.GetAllPendingBunkerInventory().Count;
                    int pendingSoftmarVoyageMaster = voyageService.GetAllPendingVoyageMaster().Count;
                    int pendingWinSDSPO = winsdsService.GetAllPendingWinSDSPO().Count;

                    IList users = m.LoadAllUser();

                    string file = ConfigurationManager.AppSettings["StartPath"] + "EmailBody.html";
                    StreamReader rd = new StreamReader(file);
                    string body = rd.ReadToEnd();
                    rd.Close();
                    string subject = "Agresso Interface : ";
                    string softmarPending = string.Format(@"SOFTMAR :<br> 
                                        - Commission Invoice : {0} item(s)<br> 
                                        - Freight Hire Invoice : {1} item(s)<br> 
                                        - Statement Invoice : {2} item(s)<br> 
                                        - Bunker PO : {3} item(s)<br> 
                                        - Bunker GRN : {4} item(s)<br> 
                                        - Bunker Inventory : {5} item(s)<br> 
                                        - Voyage Master : {6} item(s)<br> 
                <br>", pendingSoftmarCommissionInvoice, pendingSoftmarFreightHireInvoice, pendingSoftmarStatement
                        , pendingSoftmarBunkerPO, pendingSoftmarBunkerGRN, pendingSoftmarBunkerInventory, pendingSoftmarVoyageMaster);
                    string winSDSPending = string.Format(@"WINSDS :<br>
                 - Purchase Order : {0} items<br>
                 <br>", pendingWinSDSPO);

                    foreach (User user in users)
                    {
                        if (user.SendAlertToEmail)
                        {
                            if (user.EmailID != "")
                            {
                                string alert = (user.SoftmarAccess ? softmarPending : "") + (user.WinSDSAccess ? winSDSPending : "");
                                EmailService.SendByLocalServer(subject + (user.SoftmarAccess ? "SOFTMAR " : "") + (user.WinSDSAccess ? "WINSDS " : "")
                                    , string.Format(body, alert), user.EmailID);
                            }
                        }
                    }
                    m_lastExecuteDateEmailAlert = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute Email ALert on {0} done", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute Email Alert", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute Email Alert : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateWinSDSPO = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteBunkerInventory()
        {
            if (m_ExecuteDateBunkerInventory) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("BunkerInventory");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateBunkerInventory = true;
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateBunkerInventory == time;
                if (!lastexecute)
                {
                    evt.WriteEntry(string.Format("ImportAllDataSoftmarToMiddleLayer Bunker Inventory Interface on {0}", DateTime.Now));
                    BunkerInventoryService bunkerINvService = new BunkerInventoryService();
                    bunkerINvService.ImportAllDataSoftmarToMiddleLayerBODandBOR();
                    bunkerINvService.ImportAllDataSoftmarToMiddleLayerTotalConsumption();
                    evt.WriteEntry(string.Format("ImportAllDataSoftmarToMiddleLayer Bunker Inventory done on {0}", DateTime.Now));
                    evt.WriteEntry(string.Format("Execute Bunker Inventory ExecuteInterfaceIWTotalConsumption Interface start on {0}", DateTime.Now));
                    bunkerINvService.ExecuteInterfaceIWTotalConsumption();
                    evt.WriteEntry(string.Format("Execute Bunker Inventory ExecuteInterfaceIWBunkerOnDelivery Interface start on {0}", DateTime.Now));
                    bunkerINvService.ExecuteInterfaceIWBunkerOnDelivery();
                    evt.WriteEntry(string.Format("Execute Bunker Inventory ExecuteInterfaceIRBunkerOnReDelivery Interface start on {0}", DateTime.Now));
                    bunkerINvService.ExecuteInterfaceIRBunkerOnReDelivery();
                    m_lastExecuteDateBunkerInventory = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute  Bunker Inventory Interface end on {0}", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute Bunker Inventory", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while EExecute Bunker Inventory : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateBunkerInventory = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
        private void ExecuteCrewPayroll()
        {
            if (m_ExecuteDateCrewPayroll) return;
            ConfigurationSettingService confService = new ConfigurationSettingService();
            ConfigurationSetting cs = confService.GetConfigurationSetting("CrewPayroll");

            DateTime lastExecute = Convert.ToDateTime(cs.val_10);//get last execute----
            lastExecute = new DateTime(lastExecute.Year, lastExecute.Month, lastExecute.Day, lastExecute.Hour, lastExecute.Minute, 0);//remove second last execute
            DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);//get timenow without second
            if (lastExecute == time) return;//if last execute == now return---

            cs.val_12 = "RUNNING";
            confService.UpdateConfiguration(cs);
            try
            {
                m_ExecuteDateCrewPayroll = true;
                //DateTime time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                bool lastexecute = m_lastExecuteDateCrewPayroll == time;
                if (!lastexecute)
                {
                    evt.WriteEntry(string.Format("Execute Crew Payroll Interface start on {0}", DateTime.Now));
                    CrewPayrollService cpvService = new CrewPayrollService();
                    cpvService.ExecuteCrewPayrollInterface();
                    m_lastExecuteDateCrewPayroll = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);
                    evt.WriteEntry(string.Format("Execute Crew Payroll Interface start on {0}", DateTime.Now));
                }
            }
            catch (Exception x)
            {
                EmailService.SendFatalErrorToDeveloper("Error in Production System while Execute Crew Payroll", x.Message, "widagdo.baskoro2@ddw-paxocean.com");
                evt.WriteEntry(string.Format("Error in Production System while Execute Crew Payroll : {0}", x.Message));
            }
            finally
            {
                m_ExecuteDateCrewPayroll = false;
                cs.val_12 = "STOP";
                cs.val_10 = DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss");
                confService.UpdateConfiguration(cs);
            }
        }
    }
}
